{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "958e8e48e25b17a1",
   "metadata": {},
   "source": [
    "# 具体步骤\n",
    "## 1.获取数据集\n",
    "## 2.基本数据处理\n",
    "## 2.1.缩小数据范围\n",
    "## 2.2.选择时间特征\n",
    "## 2.3.去掉签到较少的地方\n",
    "## 2.4.确定特征值和目标值\n",
    "## 2.5.分割数据集\n",
    "## 3.特征工程 -- 特征预处理（标准化）\n",
    "## 4.机器学习 -- knn+cv\n",
    "## 5.模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a534135d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split,GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdb16d92",
   "metadata": {},
   "source": [
    "### 1.获取数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {},
   "outputs": [],
   "source": [
    "data=pd.read_csv(\"D:\\\\data\\\\facebook\\\\train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bfa297cdecb6714e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b2f180734ebd75e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>4.999770e+00</td>\n",
       "      <td>5.001814e+00</td>\n",
       "      <td>8.284912e+01</td>\n",
       "      <td>4.170104e+05</td>\n",
       "      <td>5.493787e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.405649e+06</td>\n",
       "      <td>2.857601e+00</td>\n",
       "      <td>2.887505e+00</td>\n",
       "      <td>1.147518e+02</td>\n",
       "      <td>2.311761e+05</td>\n",
       "      <td>2.611088e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000016e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.279505e+06</td>\n",
       "      <td>2.534700e+00</td>\n",
       "      <td>2.496700e+00</td>\n",
       "      <td>2.700000e+01</td>\n",
       "      <td>2.030570e+05</td>\n",
       "      <td>3.222911e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>5.009100e+00</td>\n",
       "      <td>4.988300e+00</td>\n",
       "      <td>6.200000e+01</td>\n",
       "      <td>4.339220e+05</td>\n",
       "      <td>5.518573e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.183852e+07</td>\n",
       "      <td>7.461400e+00</td>\n",
       "      <td>7.510300e+00</td>\n",
       "      <td>7.500000e+01</td>\n",
       "      <td>6.204910e+05</td>\n",
       "      <td>7.764307e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.033000e+03</td>\n",
       "      <td>7.862390e+05</td>\n",
       "      <td>9.999932e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             row_id             x             y      accuracy          time  \\\n",
       "count  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07   \n",
       "mean   1.455901e+07  4.999770e+00  5.001814e+00  8.284912e+01  4.170104e+05   \n",
       "std    8.405649e+06  2.857601e+00  2.887505e+00  1.147518e+02  2.311761e+05   \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  1.000000e+00   \n",
       "25%    7.279505e+06  2.534700e+00  2.496700e+00  2.700000e+01  2.030570e+05   \n",
       "50%    1.455901e+07  5.009100e+00  4.988300e+00  6.200000e+01  4.339220e+05   \n",
       "75%    2.183852e+07  7.461400e+00  7.510300e+00  7.500000e+01  6.204910e+05   \n",
       "max    2.911802e+07  1.000000e+01  1.000000e+01  1.033000e+03  7.862390e+05   \n",
       "\n",
       "           place_id  \n",
       "count  2.911802e+07  \n",
       "mean   5.493787e+09  \n",
       "std    2.611088e+09  \n",
       "min    1.000016e+09  \n",
       "25%    3.222911e+09  \n",
       "50%    5.518573e+09  \n",
       "75%    7.764307e+09  \n",
       "max    9.999932e+09  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0053f9b0a97ad66",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29118021, 6)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e19450f",
   "metadata": {},
   "source": [
    "## 2.基本数据处理\n",
    "\n",
    "### 2.1.缩小数据范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "41b58569",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7279514, 6)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebookData = data.query(\"x>0.0 & x<5.0 & y>0.0 &y<5.0\")\n",
    "# facebookData = data\n",
    "facebookData.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8af64a40",
   "metadata": {},
   "source": [
    "### 2.2. 选择时间特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebad31d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    470702\n",
       "1    186555\n",
       "2    322648\n",
       "3    704587\n",
       "4    472130\n",
       "Name: time, dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebookData[\"time\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b9386b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4          1970-01-06 11:08:50\n",
       "5          1970-01-03 01:27:45\n",
       "10         1970-01-01 05:55:53\n",
       "12         1970-01-07 15:34:48\n",
       "16         1970-01-10 00:06:22\n",
       "                   ...        \n",
       "29117990   1970-01-01 15:33:20\n",
       "29117991   1970-01-01 03:48:42\n",
       "29117992   1970-01-05 13:01:06\n",
       "29117995   1970-01-02 09:42:21\n",
       "29118009   1970-01-07 04:51:57\n",
       "Name: time, Length: 7279514, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time=pd.to_datetime(facebookData[\"time\"],unit=\"s\")\n",
    "time #此时依然是时间戳时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "490cee1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-06 11:08:50', '1970-01-03 01:27:45',\n",
       "               '1970-01-01 05:55:53', '1970-01-07 15:34:48',\n",
       "               '1970-01-10 00:06:22', '1970-01-01 01:44:09',\n",
       "               '1970-01-05 18:17:17', '1970-01-06 19:01:19',\n",
       "               '1970-01-04 10:40:23', '1970-01-04 14:18:54',\n",
       "               ...\n",
       "               '1970-01-07 06:52:32', '1970-01-05 13:40:54',\n",
       "               '1970-01-07 13:17:53', '1970-01-06 11:58:30',\n",
       "               '1970-01-06 00:53:04', '1970-01-01 15:33:20',\n",
       "               '1970-01-01 03:48:42', '1970-01-05 13:01:06',\n",
       "               '1970-01-02 09:42:21', '1970-01-07 04:51:57'],\n",
       "              dtype='datetime64[ns]', name='time', length=7279514, freq=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.DatetimeIndex(time) #转换成能直接用的时间\n",
    "time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1a8e2749",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([18, 17, 19, 16, 21,  3,  3,  3, 18,  7,\n",
       "       ...\n",
       "       20,  9,  4, 22, 23, 12, 15, 20,  9, 20],\n",
       "      dtype='int32', name='time', length=71664)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.hour# 现在可以日期时间进行操作了"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "878e6781",
   "metadata": {},
   "source": [
    "### 2.2.选择时间特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e3b39cf4",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_12324\\1795152227.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  facebookData[\"weekday\"] = time.weekday\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_12324\\1795152227.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  facebookData[\"day\"] = time.day\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_12324\\1795152227.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  facebookData[\"hour\"] = time.hour\n"
     ]
    }
   ],
   "source": [
    "facebookData[\"weekday\"] = time.weekday\n",
    "facebookData[\"day\"] = time.day\n",
    "facebookData[\"hour\"] = time.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3f064030",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>weekday</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>3.8099</td>\n",
       "      <td>1.9586</td>\n",
       "      <td>75</td>\n",
       "      <td>178065</td>\n",
       "      <td>6289802927</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>2.0173</td>\n",
       "      <td>4.8627</td>\n",
       "      <td>6</td>\n",
       "      <td>21353</td>\n",
       "      <td>8684462954</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>12</td>\n",
       "      <td>0.8829</td>\n",
       "      <td>1.3445</td>\n",
       "      <td>64</td>\n",
       "      <td>574488</td>\n",
       "      <td>7652380351</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>16</td>\n",
       "      <td>3.2494</td>\n",
       "      <td>3.2096</td>\n",
       "      <td>75</td>\n",
       "      <td>777982</td>\n",
       "      <td>2123587484</td>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117990</th>\n",
       "      <td>29117990</td>\n",
       "      <td>3.8946</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>54</td>\n",
       "      <td>56000</td>\n",
       "      <td>5424060898</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117991</th>\n",
       "      <td>29117991</td>\n",
       "      <td>1.4695</td>\n",
       "      <td>4.7915</td>\n",
       "      <td>170</td>\n",
       "      <td>13722</td>\n",
       "      <td>9518416844</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117992</th>\n",
       "      <td>29117992</td>\n",
       "      <td>4.3447</td>\n",
       "      <td>3.6639</td>\n",
       "      <td>60</td>\n",
       "      <td>392466</td>\n",
       "      <td>4579210194</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117995</th>\n",
       "      <td>29117995</td>\n",
       "      <td>1.3297</td>\n",
       "      <td>2.4151</td>\n",
       "      <td>157</td>\n",
       "      <td>121341</td>\n",
       "      <td>1226687693</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118009</th>\n",
       "      <td>29118009</td>\n",
       "      <td>3.4611</td>\n",
       "      <td>3.2491</td>\n",
       "      <td>68</td>\n",
       "      <td>535917</td>\n",
       "      <td>8647480716</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7279514 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id  weekday  \\\n",
       "4                4  4.0961  1.1307        31  472130  7440663949        1   \n",
       "5                5  3.8099  1.9586        75  178065  6289802927        5   \n",
       "10              10  2.0173  4.8627         6   21353  8684462954        3   \n",
       "12              12  0.8829  1.3445        64  574488  7652380351        2   \n",
       "16              16  3.2494  3.2096        75  777982  2123587484        5   \n",
       "...            ...     ...     ...       ...     ...         ...      ...   \n",
       "29117990  29117990  3.8946  0.1812        54   56000  5424060898        3   \n",
       "29117991  29117991  1.4695  4.7915       170   13722  9518416844        3   \n",
       "29117992  29117992  4.3447  3.6639        60  392466  4579210194        0   \n",
       "29117995  29117995  1.3297  2.4151       157  121341  1226687693        4   \n",
       "29118009  29118009  3.4611  3.2491        68  535917  8647480716        2   \n",
       "\n",
       "          day  hour  \n",
       "4           6    11  \n",
       "5           3     1  \n",
       "10          1     5  \n",
       "12          7    15  \n",
       "16         10     0  \n",
       "...       ...   ...  \n",
       "29117990    1    15  \n",
       "29117991    1     3  \n",
       "29117992    5    13  \n",
       "29117995    2     9  \n",
       "29118009    7     4  \n",
       "\n",
       "[7279514 rows x 9 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "facebookData"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19132814",
   "metadata": {},
   "source": [
    "### 2.3.去掉签到较少的地方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "26015800",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>weekday</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1000213704</th>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "      <td>170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000383269</th>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000392527</th>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "      <td>130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000472949</th>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1000505259</th>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999450678</th>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "      <td>376</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999515258</th>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999755282</th>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999851158</th>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9999855083</th>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "      <td>211</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>48281 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id    x    y  accuracy  time  weekday  day  hour\n",
       "place_id                                                        \n",
       "1000213704     170  170  170       170   170      170  170   170\n",
       "1000383269      83   83   83        83    83       83   83    83\n",
       "1000392527     130  130  130       130   130      130  130   130\n",
       "1000472949       5    5    5         5     5        5    5     5\n",
       "1000505259      97   97   97        97    97       97   97    97\n",
       "...            ...  ...  ...       ...   ...      ...  ...   ...\n",
       "9999450678     376  376  376       376   376      376  376   376\n",
       "9999515258       6    6    6         6     6        6    6     6\n",
       "9999755282      81   81   81        81    81       81   81    81\n",
       "9999851158      60   60   60        60    60       60   60    60\n",
       "9999855083     211  211  211       211   211      211  211   211\n",
       "\n",
       "[48281 rows x 8 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "placeCount=facebookData.groupby(\"place_id\").count()\n",
    "placeCount"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5c8242c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "placeCount=placeCount[placeCount[\"row_id\"]>3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c79a69a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>weekday</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>3.8099</td>\n",
       "      <td>1.9586</td>\n",
       "      <td>75</td>\n",
       "      <td>178065</td>\n",
       "      <td>6289802927</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>10</td>\n",
       "      <td>2.0173</td>\n",
       "      <td>4.8627</td>\n",
       "      <td>6</td>\n",
       "      <td>21353</td>\n",
       "      <td>8684462954</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>12</td>\n",
       "      <td>0.8829</td>\n",
       "      <td>1.3445</td>\n",
       "      <td>64</td>\n",
       "      <td>574488</td>\n",
       "      <td>7652380351</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>16</td>\n",
       "      <td>3.2494</td>\n",
       "      <td>3.2096</td>\n",
       "      <td>75</td>\n",
       "      <td>777982</td>\n",
       "      <td>2123587484</td>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117990</th>\n",
       "      <td>29117990</td>\n",
       "      <td>3.8946</td>\n",
       "      <td>0.1812</td>\n",
       "      <td>54</td>\n",
       "      <td>56000</td>\n",
       "      <td>5424060898</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117991</th>\n",
       "      <td>29117991</td>\n",
       "      <td>1.4695</td>\n",
       "      <td>4.7915</td>\n",
       "      <td>170</td>\n",
       "      <td>13722</td>\n",
       "      <td>9518416844</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117992</th>\n",
       "      <td>29117992</td>\n",
       "      <td>4.3447</td>\n",
       "      <td>3.6639</td>\n",
       "      <td>60</td>\n",
       "      <td>392466</td>\n",
       "      <td>4579210194</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29117995</th>\n",
       "      <td>29117995</td>\n",
       "      <td>1.3297</td>\n",
       "      <td>2.4151</td>\n",
       "      <td>157</td>\n",
       "      <td>121341</td>\n",
       "      <td>1226687693</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29118009</th>\n",
       "      <td>29118009</td>\n",
       "      <td>3.4611</td>\n",
       "      <td>3.2491</td>\n",
       "      <td>68</td>\n",
       "      <td>535917</td>\n",
       "      <td>8647480716</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7261562 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id       x       y  accuracy    time    place_id  weekday  \\\n",
       "4                4  4.0961  1.1307        31  472130  7440663949        1   \n",
       "5                5  3.8099  1.9586        75  178065  6289802927        5   \n",
       "10              10  2.0173  4.8627         6   21353  8684462954        3   \n",
       "12              12  0.8829  1.3445        64  574488  7652380351        2   \n",
       "16              16  3.2494  3.2096        75  777982  2123587484        5   \n",
       "...            ...     ...     ...       ...     ...         ...      ...   \n",
       "29117990  29117990  3.8946  0.1812        54   56000  5424060898        3   \n",
       "29117991  29117991  1.4695  4.7915       170   13722  9518416844        3   \n",
       "29117992  29117992  4.3447  3.6639        60  392466  4579210194        0   \n",
       "29117995  29117995  1.3297  2.4151       157  121341  1226687693        4   \n",
       "29118009  29118009  3.4611  3.2491        68  535917  8647480716        2   \n",
       "\n",
       "          day  hour  \n",
       "4           6    11  \n",
       "5           3     1  \n",
       "10          1     5  \n",
       "12          7    15  \n",
       "16         10     0  \n",
       "...       ...   ...  \n",
       "29117990    1    15  \n",
       "29117991    1     3  \n",
       "29117992    5    13  \n",
       "29117995    2     9  \n",
       "29118009    7     4  \n",
       "\n",
       "[7261562 rows x 9 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过place_id，把facebook与placeCount做对比，然后facebook的差集剔除\n",
    "facebookData=facebookData[facebookData[\"place_id\"].isin(placeCount.index)]\n",
    "facebookData"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa4898fa",
   "metadata": {},
   "source": [
    "### 2.4.确定特征值和目标值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1355321d",
   "metadata": {},
   "outputs": [],
   "source": [
    "x=facebookData[[\"x\",\"y\",\"accuracy\",\"hour\",\"day\",\"weekday\"]]\n",
    "y=facebookData[\"place_id\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c5b7f9",
   "metadata": {},
   "source": [
    "### 2.5.分割数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b367d062",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(               x       y  accuracy  hour  day  weekday\n",
       " 163       2.1663  2.3755        84    18    8        3\n",
       " 310       2.3695  2.2034         3    17    3        5\n",
       " 658       2.3236  2.1768        66    19    6        1\n",
       " 1368      2.2613  2.3392        73    16    4        6\n",
       " 1627      2.3331  2.0011        66    21    7        2\n",
       " ...          ...     ...       ...   ...  ...      ...\n",
       " 29116142  2.0804  2.0657       168    12    3        5\n",
       " 29116267  2.4309  2.4646        33    15    4        6\n",
       " 29116295  2.1797  2.1707        89    20    1        3\n",
       " 29116475  2.3924  2.2704        62     9    3        5\n",
       " 29117203  2.4942  2.2430        11    20    2        4\n",
       " \n",
       " [69264 rows x 6 columns],\n",
       " 163         3869813743\n",
       " 310         2636621520\n",
       " 658         7877745055\n",
       " 1368        9775192577\n",
       " 1627        6731326909\n",
       "                ...    \n",
       " 29116142    1247398579\n",
       " 29116267    1951613663\n",
       " 29116295    4724115005\n",
       " 29116475    2819110495\n",
       " 29117203    2634419689\n",
       " Name: place_id, Length: 69264, dtype: int64)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a837b24f",
   "metadata": {},
   "outputs": [],
   "source": [
    "trainX,testX,trainY,testY=train_test_split(x,y,random_state=2,test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f87b6987",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>19509166</th>\n",
       "      <td>2.3217</td>\n",
       "      <td>2.2029</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20577315</th>\n",
       "      <td>2.4800</td>\n",
       "      <td>2.2129</td>\n",
       "      <td>175</td>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24044078</th>\n",
       "      <td>2.2389</td>\n",
       "      <td>2.3447</td>\n",
       "      <td>79</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11279021</th>\n",
       "      <td>2.0850</td>\n",
       "      <td>2.2789</td>\n",
       "      <td>119</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19491154</th>\n",
       "      <td>2.2408</td>\n",
       "      <td>2.0092</td>\n",
       "      <td>168</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22659524</th>\n",
       "      <td>2.4458</td>\n",
       "      <td>2.1057</td>\n",
       "      <td>39</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18678003</th>\n",
       "      <td>2.0045</td>\n",
       "      <td>2.0432</td>\n",
       "      <td>71</td>\n",
       "      <td>9</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14317979</th>\n",
       "      <td>2.2588</td>\n",
       "      <td>2.3618</td>\n",
       "      <td>47</td>\n",
       "      <td>15</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13111786</th>\n",
       "      <td>2.4795</td>\n",
       "      <td>2.3323</td>\n",
       "      <td>65</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18790817</th>\n",
       "      <td>2.2873</td>\n",
       "      <td>2.2393</td>\n",
       "      <td>175</td>\n",
       "      <td>20</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>51948 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               x       y  accuracy  hour  day  weekday\n",
       "19509166  2.3217  2.2029         1     0    8        3\n",
       "20577315  2.4800  2.2129       175    20    7        2\n",
       "24044078  2.2389  2.3447        79     3    8        3\n",
       "11279021  2.0850  2.2789       119     1    9        4\n",
       "19491154  2.2408  2.0092       168     0    8        3\n",
       "...          ...     ...       ...   ...  ...      ...\n",
       "22659524  2.4458  2.1057        39     8    5        0\n",
       "18678003  2.0045  2.0432        71     9    7        2\n",
       "14317979  2.2588  2.3618        47    15    8        3\n",
       "13111786  2.4795  2.3323        65     2    2        4\n",
       "18790817  2.2873  2.2393       175    20    9        4\n",
       "\n",
       "[51948 rows x 6 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f6c7b700",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>21112721</th>\n",
       "      <td>2.4322</td>\n",
       "      <td>2.0640</td>\n",
       "      <td>569</td>\n",
       "      <td>17</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15305275</th>\n",
       "      <td>2.1001</td>\n",
       "      <td>2.4034</td>\n",
       "      <td>66</td>\n",
       "      <td>15</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2286447</th>\n",
       "      <td>2.0855</td>\n",
       "      <td>2.1071</td>\n",
       "      <td>257</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2683519</th>\n",
       "      <td>2.0240</td>\n",
       "      <td>2.3505</td>\n",
       "      <td>51</td>\n",
       "      <td>14</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>276963</th>\n",
       "      <td>2.3625</td>\n",
       "      <td>2.3676</td>\n",
       "      <td>81</td>\n",
       "      <td>15</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5377682</th>\n",
       "      <td>2.1865</td>\n",
       "      <td>2.1799</td>\n",
       "      <td>61</td>\n",
       "      <td>18</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5778143</th>\n",
       "      <td>2.2492</td>\n",
       "      <td>2.4795</td>\n",
       "      <td>201</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10652670</th>\n",
       "      <td>2.4178</td>\n",
       "      <td>2.2196</td>\n",
       "      <td>70</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5835344</th>\n",
       "      <td>2.2750</td>\n",
       "      <td>2.2136</td>\n",
       "      <td>46</td>\n",
       "      <td>14</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12327543</th>\n",
       "      <td>2.2197</td>\n",
       "      <td>2.4945</td>\n",
       "      <td>70</td>\n",
       "      <td>22</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>17316 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               x       y  accuracy  hour  day  weekday\n",
       "21112721  2.4322  2.0640       569    17    8        3\n",
       "15305275  2.1001  2.4034        66    15    5        0\n",
       "2286447   2.0855  2.1071       257     0    5        0\n",
       "2683519   2.0240  2.3505        51    14    1        3\n",
       "276963    2.3625  2.3676        81    15    9        4\n",
       "...          ...     ...       ...   ...  ...      ...\n",
       "5377682   2.1865  2.1799        61    18    8        3\n",
       "5778143   2.2492  2.4795       201     1    5        0\n",
       "10652670  2.4178  2.2196        70    20    2        4\n",
       "5835344   2.2750  2.2136        46    14    5        0\n",
       "12327543  2.2197  2.4945        70    22    9        4\n",
       "\n",
       "[17316 rows x 6 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testX"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "958bfb90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19509166    8635054095\n",
       "20577315    4561616726\n",
       "24044078    5882510939\n",
       "11279021    9363910628\n",
       "19491154    3539133103\n",
       "               ...    \n",
       "22659524    7511942547\n",
       "18678003    5014521982\n",
       "14317979    8612356943\n",
       "13111786    8421868147\n",
       "18790817    5547923718\n",
       "Name: place_id, Length: 51948, dtype: int64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "af9176e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21112721    2225211839\n",
       "15305275    4634909749\n",
       "2286447     3692608023\n",
       "2683519     2870156931\n",
       "276963      5967678921\n",
       "               ...    \n",
       "5377682     9311396375\n",
       "5778143     2745931062\n",
       "10652670    1891783132\n",
       "5835344     8169595806\n",
       "12327543    8950308472\n",
       "Name: place_id, Length: 17316, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testY"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60688502",
   "metadata": {},
   "source": [
    "## 3.特征工程 -- 特征预处理：标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "49824bf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "transfer = StandardScaler()\n",
    "trainX = transfer.fit_transform(trainX)\n",
    "testX = transfer.fit_transform(testX)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6f4601f",
   "metadata": {},
   "source": [
    "## 4.机器学习 knn + cv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b108fe8b",
   "metadata": {},
   "source": [
    "### 4.1. 实例化训练器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b14b7030",
   "metadata": {},
   "outputs": [],
   "source": [
    "estimator=KNeighborsClassifier()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3c46fc3",
   "metadata": {},
   "source": [
    "### 4.2.交叉验证，网格搜索\n",
    "### 4.3.训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f10c702e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\ProgramData\\anaconda3\\Lib\\site-packages\\sklearn\\model_selection\\_split.py:725: UserWarning: The least populated class in y has only 1 members, which is less than n_splits=5.\n",
      "  warnings.warn(\n",
      "Exception in thread ExecutorManagerThread:\n",
      "Traceback (most recent call last):\n",
      "  File \"F:\\ProgramData\\anaconda3\\Lib\\threading.py\", line 1038, in _bootstrap_inner\n",
      "    self.run()\n",
      "  File \"F:\\ProgramData\\anaconda3\\Lib\\site-packages\\joblib\\externals\\loky\\process_executor.py\", line 557, in run\n",
      "    result_item, is_broken, bpe = self.wait_result_broken_or_wakeup()\n",
      "                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"F:\\ProgramData\\anaconda3\\Lib\\site-packages\\joblib\\externals\\loky\\process_executor.py\", line 611, in wait_result_broken_or_wakeup\n",
      "    ready = wait(readers + worker_sentinels)\n",
      "            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"F:\\ProgramData\\anaconda3\\Lib\\multiprocessing\\connection.py\", line 878, in wait\n",
      "    ready_handles = _exhaustive_wait(waithandle_to_obj.keys(), timeout)\n",
      "                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"F:\\ProgramData\\anaconda3\\Lib\\multiprocessing\\connection.py\", line 810, in _exhaustive_wait\n",
      "    res = _winapi.WaitForMultipleObjects(L, False, timeout)\n",
      "          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "ValueError: need at most 63 handles, got a sequence of length 73\n"
     ]
    }
   ],
   "source": [
    "### 4.2.交叉验证，网格搜索\n",
    "param_grid = {\"n_neighbors\": [ 3, 5, 7, 9]} # 当数据量比较多的时候，超参数一定不要传太多！！！\n",
    "_estimator = GridSearchCV(estimator=estimator, param_grid=param_grid, cv=5, n_jobs=72,verbose=1) # n_job=-1 表示使用所有CPU核，然鹅现在报错？\n",
    "\n",
    "### 4.3.训练模型\n",
    "_estimator.fit(trainX,trainY)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77fb38dd",
   "metadata": {},
   "source": [
    "## 5.模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cc434ded",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率= 0.16272637685214922\n",
      "预测结果= [2161810178 8049455297 2216867795 ... 5034663749 1559545739 4934570083]\n",
      "交叉验证中最好模型= KNeighborsClassifier(n_neighbors=3)\n",
      "交叉验证中最好结果= 0.1353718052710237\n",
      "交叉验证中得到的所有模型结果= {'mean_fit_time': array([25.47257288, 23.86444815, 24.78035259]), 'std_fit_time': array([0.18261222, 1.0274864 , 1.16854162]), 'mean_score_time': array([281.7373035 , 302.79002762, 332.34052372]), 'std_score_time': array([0.90547674, 7.74833829, 9.54840333]), 'param_n_neighbors': masked_array(data=[3, 5, 7],\n",
      "             mask=[False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}], 'split0_test_score': array([0.13528711, 0.12986514, 0.12672972]), 'split1_test_score': array([0.13552956, 0.13025906, 0.1267353 ]), 'split2_test_score': array([0.13529875, 0.13009161, 0.12644886]), 'mean_test_score': array([0.13537181, 0.13007193, 0.12663796]), 'std_test_score': array([0.00011165, 0.00016142, 0.00013373]), 'rank_test_score': array([1, 2, 3])}\n"
     ]
    }
   ],
   "source": [
    "# 第一次\n",
    "print(\"准确率=\",_estimator.score(testX,testY))\n",
    "print(\"预测结果=\",_estimator.predict(testX))\n",
    "print(\"交叉验证中最好模型=\", _estimator.best_estimator_)\n",
    "print(\"交叉验证中最好结果=\", _estimator.best_score_)\n",
    "print(\"交叉验证中得到的所有模型结果=\", _estimator.cv_results_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfc22f81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二次\n",
    "print(\"准确率=\",_estimator.score(testX,testY))\n",
    "print(\"预测结果=\",_estimator.predict(testX))\n",
    "print(\"交叉验证中最好模型=\", _estimator.best_estimator_)\n",
    "print(\"交叉验证中最好结果=\", _estimator.best_score_)\n",
    "print(\"交叉验证中得到的所有模型结果=\", _estimator.cv_results_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3ec9f59",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
